package com.bear.cmcc.NetTestHTTP.util;

import com.bear.cmcc.NetTestHTTP.component.CountrySortBean;

import java.util.Comparator;

@SuppressWarnings("rawtypes")
public class Sort2GSpeed implements Comparator{
	public final static int UP = 1;
    public final static int DOWN = -1;
    private int state;
   
    /**
     * 2G排序
     * @param state 排序状态值
     */
    public Sort2GSpeed(int state) {
        this.state = state;
    }
    
	public int compare(Object obj1, Object obj2) {
		CountrySortBean ps1 = (CountrySortBean)obj1;
		CountrySortBean ps2 = (CountrySortBean)obj2;
		if (state == Sort2GSpeed.DOWN) {
			return sortDonw(ps1.gprs_downSpeed, ps2.gprs_downSpeed);
		}	
			return sortUp(ps1.gprs_downSpeed, ps2.gprs_downSpeed);
	}
	
	/**
	 * 升序
	 * @param sp1
	 * @param sp2
	 * @return
	 */
	private int sortUp(String sp1,String sp2)
	{
		Double d1=0.0,d2=0.0;
		try{
			d1 = Double.valueOf(sp1);
			d2 = Double.valueOf(sp2);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(d1.compareTo(d2) < 0)
			return -1;
		else if(d1.compareTo(d2) > 0)
			return 1;
		else
			return 0;
	}
	
	/**
	 * 降序
	 * @param sp1
	 * @param sp2
	 * @return
	 */
	private int sortDonw(String sp1,String sp2)
	{
		Double d1=0.0,d2=0.0;
		try{
			d1 = Double.valueOf(sp1);
			d2 = Double.valueOf(sp2);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(d1.compareTo(d2) > 0)
			return -1;
		else if(d1.compareTo(d2) < 0)
			return 1;
		else
			return 0;
	}

}
